package com.shitou.shitoucart.controller;

import com.shitou.shitoucart.entity.CartEntity;
import com.shitou.shitoucart.vo.CartVo;
import com.shitou.shitoucommons.annotation.CheckUserLogin;
import com.shitou.shitoucommons.utils.PageUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.shitou.shitoucart.service.CartService;
import com.shitou.shitoucommons.utils.RespBean;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Map;


/**
 * 购物车-cat
 *
 * @author shitouren
 * @email 3076767823@qq.com
 * @date 2023-03-20 00:56:24
 */
@RestController
@RequestMapping("shitoucart/cart")
public class CartController {
    @Autowired
    private CartService cartService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public RespBean list(@RequestParam Map<String, Object> params){
        PageUtils page = cartService.queryPage(params);

        return RespBean.success().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public RespBean info(@PathVariable("attrId") Long attrId){
        CartEntity attr = cartService.getById(attrId);

        return RespBean.success().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public RespBean save(@RequestBody CartEntity attr){
        cartService.save(attr);

        return RespBean.success();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public RespBean update(@RequestBody CartEntity attr){
        cartService.updateById(attr);

        return RespBean.success();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public RespBean delete(@RequestBody Long[] attrIds){
        cartService.removeByIds(Arrays.asList(attrIds));

        return RespBean.success();
    }

    /**
     * 添加，创建购物车信息
     * @param cartVo 映射类
     * @return RespBean
     */
    @ApiOperation(value = "会员将商品添加入购物车创建购物车时调用，需要登录校验")
    @CheckUserLogin
    @PostMapping("/addMemberCart")
    public RespBean addMemberCart(@RequestBody CartVo cartVo, HttpServletRequest request, HttpServletResponse response){
        return cartService.addCart(cartVo, request, response);
    }


    /**
     * 获得用户的购物车
     * @param memberId 用户的id
     * @return RespBean
     */
    @ApiOperation(value = "获得当前登录会员的所有购物车信息，需要登录校验")
    @CheckUserLogin
    @PostMapping("/getMemberCart/{memberId}")
    public RespBean getMemberCart(@PathVariable("memberId") long memberId, HttpServletRequest request, HttpServletResponse response){
        return cartService.getMemberCart(memberId, request, response);
    }


    /**
     * 删除购物车
     * @param cartId 购物车id
     * @return RespBean
     */
    @ApiOperation(value = "删除购物车中的单个商品，通过购物车id，需要登录校验")
    @CheckUserLogin
    @PostMapping("/removeMemberCart")
    public RespBean removeMemberCart(@RequestParam long cartId,@RequestParam long memberId){
        return cartService.removeMemberCart(cartId, memberId);
    }


    /**
     * 购物车的模糊搜搜
     * @param searchInfo 搜索的内容信息
     * @param memberId 用户id
     * @return RespBean
     */
    @ApiOperation(value = "对购物车进行模糊搜索查询，需要登录校验")
    @CheckUserLogin
    @PostMapping("/searchCart")
    public RespBean searchCart(@RequestParam String searchInfo, @RequestParam long memberId, HttpServletRequest request, HttpServletResponse response){
        return cartService.searchCart(searchInfo, memberId, request, response);
    }


}
